//
//  RYProductCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/4.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import YYText

class RYProductCell: SYBaseCell {

    fileprivate var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5.0.autoWidth()
        return v
    }()
    
    let titleLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1D1D1D)
        v.font = 16.autoFontSize()
        
        return v
    }()
    
    let biaohaoLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor =  UIColor(hex: 0x999999)
        v.font = 12.autoFontSize()
        
        return v
    }()
    
    let zhouqiLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x999999)
        v.font = 12.autoFontSize()
        
        return v
    }()
    
    let shengyuLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x111111)
        v.font = 13.autoFontSize()
        
        return v
    }()
    
    let jinquPic: UIImageView = {
        let v: UIImageView = UIImageView()
        v.backgroundColor = .blue
        return v
    }()
    
    let pic: UIImageView = {
        let v: UIImageView = UIImageView()
        
        
        return v
    }()
    
    let jinduPic: RYJingduView = {
        let v: RYJingduView = RYJingduView()
        v.totalWidth = 108.5.autoWidth()
        return v
    }()
    
    let renyang: UIButton = {
        let v: UIButton  = UIButton()
        v.setTitle("立即认养", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = 14.autoFontSize()
        v.layer.cornerRadius = 3
//        v.vg_gradLayerCreatWithFrame(frame: CGRect(x: 0, y: 0, width: 70.0.autoWidth(), height: 28.0.autoWidth()), leftC: UIColor(hex: 0xFF7F00), rightC: UIColor(hex: 0xFF9700))
        v.setImage(UIImage(named: "renyang_bt"), for: .normal)
        return v
    }()
    
    let jiageLb: YYLabel = {
        let v:YYLabel = YYLabel()
        v.textColor = UIColor(hex: 0x111111)
        v.font = 13.autoFontSize()
        v.textAlignment = .center
        return v
    }()
    
    let jiageTitleLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x111111)
        v.font = 13.autoFontSize()
        v.text = "认养价格"
        v.textAlignment = .center
        return v
    }()
    
    let chanliangLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x111111)
        v.font = 13.autoFontSize()
        v.textAlignment = .center
        return v
    }()
    
    let chanliangTitleLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x111111)
        v.font = 13.autoFontSize()
        v.text = "产品产量"
        v.textAlignment = .center
        return v
    }()
    
    let danjiaLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x111111)
        v.font = 13.autoFontSize()
        v.textAlignment = .center
        return v
    }()
    
    let danjiaTitleLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x111111)
        v.font = 13.autoFontSize()
        v.textAlignment = .center
        v.text = "产品单价"
        return v
    }()

    public func configureView(_ model: RYProductListModel) {
        titleLb.text = model.name
        biaohaoLb.text = "编号:\(model.serialNumber ?? "")"
        zhouqiLb.text = "作物生长周期:\(model.productionCycle ?? 0)天"
        
        if let b = model.adoptedPercent {
            let bai = b.replacingOccurrences(of: "%", with: "")
            let dou = bai.doubleValue/100
            jinduPic.currentJindu = dou.saveTwo
        } else {
            jinduPic.currentJindu = 0.01
        }
        shengyuLb.text = "剩余:\(model.remainNum ?? 0)份"
        
        let firStr = "\(model.adoptPrice?.saveTwo ?? "0")"
        let totalStr = "\(firStr)元/\(model.adoptPart ?? "")"
        let attString = NSMutableAttributedString(string: totalStr)
        attString.yy_setColor(UIColor(hex: 0xF55035), range: NSRange(location: 0, length: firStr.count))
        attString.yy_setFont(16.autoFontSize(), range: NSRange(location: 0, length:firStr.count))
        attString.yy_setColor(UIColor(hex: 0x111111), range: NSRange(location: firStr.count, length: totalStr.count - firStr.count))
        attString.yy_setFont(13.autoFontSize(), range: NSRange(location: firStr.count, length: totalStr.count - firStr.count))
        attString.yy_setAlignment(.center, range: NSRange(location: 0, length: totalStr.count))
        
        jiageLb.attributedText = attString
        chanliangLb.text = model.adoptHarvest
        danjiaLb.text = "\(model.unitPrice?.saveTwo ?? "0")元/斤"
        if let p = model.detailImgUrl {
            let urls = p.components(separatedBy: ",")
            let url = urls.first ?? ""
            pic.kf.setImage(with: URL(string: IMGIP+url))
        }
    }
    
    override func initViews() {
        
        
        
        self.backgroundColor = UIColor(hex: 0xF2F2F2)
        addSubview(bgView)
        bgView.addSubview(pic)
        bgView.addSubview(titleLb)
        bgView.addSubview(biaohaoLb)
        bgView.addSubview(zhouqiLb)
        bgView.addSubview(shengyuLb)
        
        bgView.addSubview(jinduPic)
        bgView.addSubview(renyang)
        bgView.addSubview(jiageLb)
        bgView.addSubview(jiageTitleLb)
        
        bgView.addSubview(chanliangLb)
        bgView.addSubview(chanliangTitleLb)
        bgView.addSubview(danjiaLb)
        bgView.addSubview(danjiaTitleLb)
        bgView.addSubview(lineView)
        
        let itemWidth: Double = Double(SCREEN_WIDTH / 3)
        
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.bottom.equalToSuperview()
        }
        pic.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(autoWidth(15.0))
            make.left.equalToSuperview().offset(autoWidth(10.0))
            make.width.height.equalTo(100.0.autoWidth())
        }
        titleLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(autoWidth(17.5))
            make.left.equalTo(pic.snp.right).offset(9.0.autoWidth())
            make.height.equalTo(15.5.autoWidth())
        }
        biaohaoLb.snp.makeConstraints { (make) in
            make.top.equalTo(titleLb.snp.bottom).offset(autoWidth(13.5))
            make.left.equalTo(titleLb.snp.left)
            make.height.equalTo(11.5.autoWidth())
        }
        zhouqiLb.snp.makeConstraints { (make) in
            make.top.equalTo(biaohaoLb.snp.bottom).offset(autoWidth(11.5))
            make.left.equalTo(titleLb.snp.left)
            make.height.equalTo(11.5.autoWidth())
        }
        shengyuLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(zhouqiLb)
            make.height.equalTo(13.0.autoWidth())
            make.right.equalToSuperview().offset(autoWidth(-11.0))
        }
        
        jinduPic.snp.makeConstraints { (make) in
            make.bottom.equalTo(pic.snp.bottom)
            make.left.equalTo(pic.snp.right).offset(4.5.autoWidth())
            make.height.equalTo(26.5.autoWidth())
            make.width.equalTo(108.5.autoWidth())
        }
        renyang.snp.makeConstraints { (make) in
            make.bottom.equalTo(pic.snp.bottom)
            make.right.equalToSuperview().offset(autoWidth(-10.0))
            make.height.equalTo(28.0.autoWidth())
            make.width.equalTo(70.0.autoWidth())
        }
        
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(pic.snp.bottom).offset(autoWidth(14.5))
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        jiageLb.snp.makeConstraints { (make) in
            make.top.equalTo(pic.snp.bottom).offset(autoWidth(27.5))
            make.left.equalToSuperview()
            make.height.equalTo(13.5.autoWidth())
            make.width.equalTo(itemWidth.autoWidth())
        }
        jiageTitleLb.snp.makeConstraints { (make) in
            make.centerX.equalTo(jiageLb)
            make.top.equalTo(jiageLb.snp.bottom).offset(autoWidth(11.5))
            make.height.equalTo(13.0.autoWidth())
            make.width.equalTo(itemWidth.autoWidth())
        }
        
        chanliangLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(jiageLb)
            make.centerX.equalToSuperview()
            make.height.equalTo(13.5.autoWidth())
            make.width.equalTo(itemWidth.autoWidth())
        }
        chanliangTitleLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(jiageTitleLb)
            make.centerX.equalToSuperview()
            make.height.equalTo(13.0.autoWidth())
            make.width.equalTo(itemWidth.autoWidth())
        }
        danjiaLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(jiageLb)
            make.right.equalToSuperview()
            make.height.equalTo(13.5.autoWidth())
            make.width.equalTo(itemWidth.autoWidth())
        }
        danjiaTitleLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(jiageTitleLb)
            make.centerX.equalTo(danjiaLb)
            make.right.equalToSuperview()
            make.height.equalTo(13.0.autoWidth())
            make.width.equalTo(itemWidth.autoWidth())
            make.bottom.equalToSuperview().offset(-15.0.autoWidth())
        }
    }
}
